package bookshop.controller.backend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Customer;
import bookshop.domain.Manager;
import bookshop.domain.OrderInfo;
import bookshop.domain.OrderItem;
import bookshop.domain.OrderTrace;
import bookshop.service.ManagerService;
import bookshop.service.OrderService;
import bookshop.service.OrderTraceService;
import bookshop.service.helper.OrderQueryHelper;
import bookshop.service.helper.OrderTraceQueryHelper;
import bookshop.service.impl.ManagerServiceImpl;
import bookshop.service.impl.OrderServiceImpl;
import bookshop.service.impl.OrderTraceServiceImpl;
import bookshop.utils.Page;

public class ManageOrderServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String act = request.getParameter("act");

		if ("qryOrder".equals(act)) {
			OrderQueryHelper helper = new OrderQueryHelper();
			
			if(request.getParameter("qryOrderId")!=null&&request.getParameter("qryOrderId").length()!=0){
				helper.setQryOrderId(request.getParameter("qryOrderId"));
			}
			
			if(request.getParameter("qryOrderYear")!=null&&request.getParameter("qryOrderYear").length()!=0){
				helper.setQryOrderYear(request.getParameter("qryOrderYear"));
			}
			
			if(request.getParameter("qryOrderMonth")!=null&&request.getParameter("qryOrderMonth").length()!=0){
				helper.setQryOrderMonth(request.getParameter("qryOrderMonth"));
			}
			
			if(request.getParameter("qryOrderDay")!=null&&request.getParameter("qryOrderDay").length()!=0){
				helper.setQryOrderDay(request.getParameter("qryOrderDay"));
			}
			
			if(request.getParameter("qryOrderStatus")!=null&&!request.getParameter("qryOrderStatus").equals("0")){
				helper.setQryOrderStatus(request.getParameter("qryOrderStatus"));
			}

			Page page = new Page();
			if (request.getParameter("pageNo") != null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));

			OrderService orderService = new OrderServiceImpl();
			page = orderService.loadOrdersByCondition(helper, page);

			List<OrderItem> orderItems = new ArrayList<OrderItem>();

			for (Object item : page.getPageContent().toArray()) {
				OrderInfo order = (OrderInfo) item;

				for (OrderItem orderItem : orderService
						.GetOrderItemByOrderId(order.getOrderId())) {
					orderItems.add(orderItem);

				}
			}

			request.setAttribute("orderItems", orderItems);
			request.setAttribute("page", page);

			request.getRequestDispatcher("jsps/backend/order/list_order.jsp")
					.forward(request, response);
		} else if("orderProcess".equals(act)){
			String orderId = request.getParameter("orderId");
			String orderStatus = request.getParameter("orderStatus");
			Manager manager = (Manager)request.getSession().getAttribute("loginedMgr");
			
			Customer customer = new Customer();
			OrderTrace orderTrace = new OrderTrace();
            orderTrace.setCustomer(customer);
			orderTrace.setManager(manager);
			orderTrace.setOrderId(orderId);
			orderTrace.setOrderStatus(orderStatus);
			orderTrace.setOperTime(new java.util.Date());
			
			OrderTraceService orderTraceService = new OrderTraceServiceImpl();
			orderTraceService.SaveOrderTrace(orderTrace);
			
			OrderService orderService = new OrderServiceImpl();
			OrderInfo orderInfo = new OrderInfo();
			orderInfo = orderService.GetOrderByOrderId(orderId);
			orderInfo.setOrderStatus(orderStatus);
			
			orderService.UpdateOrder(orderInfo);
			
			response.sendRedirect("orderMgr?act=qryOrder");
			
		}else if("showOrderTrace".equals(act)){
			
			OrderTraceQueryHelper helper = new OrderTraceQueryHelper();
			if(request.getParameter("qryOrderId")!=null&&request.getParameter("qryOrderId").length()!=0)
				helper.setQryOrderId(request.getParameter("qryOrderId"));
			if(request.getParameter("qrymanager")!=null&&!request.getParameter("qrymanager").equals("0"))
				helper.setQryCustId(request.getParameter("qrymanager"));
				
				
			
			Page page = new Page();
			if (request.getParameter("pageNo") != null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
			
			OrderTraceService orderTraceService = new OrderTraceServiceImpl();
			
			request.setAttribute("page", orderTraceService.GetOrderTraceByConnection(helper, page));
			
			List<Manager> managers = new ArrayList<Manager>();
			ManagerService managerService = new ManagerServiceImpl();
			managers = managerService.loadall();
			request.setAttribute("managers", managers);
			request.getRequestDispatcher("/jsps/backend/order/list_orderTrace.jsp").forward(request, response);
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
